package com.wx.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.wx.constant.MessageConstant;
import com.wx.entity.PageResult;
import com.wx.entity.QueryPageBean;
import com.wx.entity.Result;
import com.wx.pojo.CheckItem;
import com.wx.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预约管理添加页面控制层
 */
@RestController //直接返回的结果结束一个json的字符串省略了@ResponseBody
@RequestMapping("/pages/checkitem")
public class CheckItemController {
   @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){

       // 调用接口方法
        // 通过try catch的方法来判断接口的调用是否成功
        try {
            System.out.println(checkItem);
            System.out.println(checkItemService);
            checkItemService.add(checkItem);
        } catch (Exception e) {

            e.printStackTrace();
            // 服务调用失败
           return   new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        // 调用成功
        System.out.println("调用成功");
        return  new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    /**
     * 分页查询
     */
    @RequestMapping("/pageQuery")
    public Result<PageResult> pageQueryAll(@RequestBody QueryPageBean queryPageBean){
        // 对queryPageBean进行校验
        if(queryPageBean.getQueryString()==null){
            queryPageBean.setQueryString("");
        }
        PageResult pageResult=null;
        try {
             pageResult = checkItemService.queryPage(queryPageBean);
            return new Result<PageResult>(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            // 查询失败
            return new Result<PageResult>(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    /**
     * 编辑检查项数据回显
     */
    @RequestMapping("query")
    public Result<CheckItem> pageQuerry( String id){
        System.out.println(id);
        try {
            int ItemId = Integer.parseInt(id);
            CheckItem checkItem = checkItemService.pageUpdate(ItemId);

            // 查询成功返回数据
            return new Result<CheckItem>(true,MessageConstant.ADD_CHECKITEM_SUCCESS,checkItem);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result<CheckItem>(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 更新数据
     */
    @RequestMapping("update")
    public Result pageUpdate(@RequestBody CheckItem checkItem){
        try {
            checkItemService.update(checkItem);
           return   new Result<CheckItem>(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return   new Result<CheckItem>(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }
    /**
     * 删除数据
     */
    @RequestMapping("delete")
    public Result deleCheckitem(String id){
        try {
            int deleteId = Integer.parseInt(id);
            checkItemService.deleteCheckitem(deleteId);
            return   new Result<CheckItem>(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return   new Result<CheckItem>(true,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}
